package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.entity.mongo.JoinPicture;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductRecord;
import org.itboys.mobile.service.mongo.site.JoinSitePictureService;
import org.itboys.mobile.service.mongo.site.SundaySiteMemberRecordService;
import org.itboys.mobile.service.mongo.site.SundaySiteMemberService;
import org.itboys.mobile.service.mysql.site.SundaySiteProductRecordService;
import org.itboys.mobile.service.mysql.site.SundaySiteProductService;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/25
 * 描述：雄安加盟网_后台审核相关
 */
@Service
public class SundayHandleService {
    @Autowired
    private SundaySiteMemberService memberService;
    @Autowired
    private SundaySiteProductService productService;
    @Autowired
    private SundaySiteMemberRecordService memberRecordService;
    @Autowired
    private SundaySiteProductRecordService productRecordService;
    @Autowired
    private SundayPermissionService permissionService;
    @Autowired
    private JoinSitePictureService pictureService;

    /**
     * 审核
     * @param memberRecordId
     */
    @Transactional
    public void handle(Long memberRecordId){
        SundayMemberRecord memberRecord=memberRecordService.getById(memberRecordId);
        if(memberRecord.getStatus()!=1){
            throw new ErrorException("请勿重复操作");
        }
        SundayProductRecord productRecord=productRecordService.findOneByMember(memberRecordId);
        SundayMember member=null;
        SundayProduct product=null;
        Long memberId=memberService.getIdByRecord(memberRecordId);
        // 如果有用户则取该记录下的用户  如果没有用户（第一次审核） 新增用户
        if(LongUtil.isNotZreo(memberId)){
            member=memberService.getById(memberId);
            product=productService.findOneByMember(memberId);

        }else {
            member=new SundayMember();
            product=new SundayProduct();
            member.setRecordId(memberRecordId);
            product.setRecordId(productRecord.getId());
            try{
                memberService.save(member);
                productService.insert(product);
                permissionService.savePermission(member.getId(),null,product.getId(),1,false);
            }catch (Exception e){
                //如果出现异常则删除
                if(LongUtil.isNotZreo(member.getId())){
                    memberService.delete(member.getId());
                }
                if(LongUtil.isNotZreo(product.getId())){
                    productService.delete(product.getId());
                }
                throw new ErrorException("网络异常");
            }
        }
        generateMemberAndProduct(memberRecord,productRecord,member,product);
        //更新记录状态
        memberRecord.setStatus(2);
        memberRecordService.updateExceptEmpty(memberRecordId,memberRecord);
        //更新图库的状态
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("memberRecordId",memberRecordId);
        List<JoinPicture> pictures=pictureService.list(param);
        if(ListUtil.isNotNull(pictures)){
            for(JoinPicture picture:pictures){
                picture.setStatus(2);
                pictureService.updateExceptEmpty(picture.getId(),picture);
            }
        }
    }

    /**
     * 审核后生成或修改   企业和项目
     * @param memberRecord
     * @param productRecord
     * @param member
     * @param product
     */
    public void generateMemberAndProduct(SundayMemberRecord memberRecord,SundayProductRecord productRecord,SundayMember member,
                                         SundayProduct product){
        //企业部分
        member.setUserName(memberRecord.getUserName());member.setNickName(memberRecord.getNickName());member.setMobile(memberRecord.getMobile());
        member.setMobileName(memberRecord.getMobileName());member.setEnglishName(memberRecord.getEnglishName());member.setPhone(memberRecord.getPhone());
        member.setQqNumber(memberRecord.getQqNumber());member.setFaxNumber(memberRecord.getFaxNumber());member.setAddress(memberRecord.getAddress());
        member.setEmail(memberRecord.getEmail());member.setUserType(memberRecord.getUserType());member.setImage(memberRecord.getImage());
        member.setIdCard(memberRecord.getIdCard());member.setBirthDay(memberRecord.getBirthDay());member.setSex(memberRecord.getSex());
        member.setMarriage(memberRecord.getMarriage());member.setDesc(memberRecord.getDesc());member.setStatus(2);
        member.setPassword(memberRecord.getPassword());member.setProvinceId(memberRecord.getProvinceId());member.setProvinceName(memberRecord.getProvinceName());
        member.setCityId(memberRecord.getCityId());member.setCityName(memberRecord.getCityName());member.setDistrictId(memberRecord.getDistrictId());member.setDistrictName(memberRecord.getDistrictName());
        member.setBackImage(memberRecord.getBackImage());member.setAmount(memberRecord.getAmount());member.setGenerateTime(memberRecord.getGenerateTime());
        member.setBusinessType(memberRecord.getBusinessType());member.setCategoryId(memberRecord.getCategoryId());member.setCategoryName(memberRecord.getCategoryName());
        member.setParentId(memberRecord.getParentId());member.setParentName(memberRecord.getParentName());member.setBusinessSite(memberRecord.getBusinessSite());
        member.setCorporation(memberRecord.getCorporation());member.setCreditNum(memberRecord.getCreditNum());member.setLicenseNum(memberRecord.getLicenseNum());
        member.setTaxNum(memberRecord.getTaxNum());member.setInstitutionalNum(memberRecord.getInstitutionalNum());member.setBrandName(memberRecord.getBrandName());
        member.setBrandEnglishName(memberRecord.getBrandEnglishName());member.setBrandLogo(memberRecord.getBrandLogo());member.setBrandSource(memberRecord.getBrandSource());
        member.setBrandGenerateTime(memberRecord.getBrandGenerateTime());member.setBrandRegisterNum(memberRecord.getBrandRegisterNum());
        member.setLicenseImg(memberRecord.getLicenseImg());member.setInstitutionalImg(memberRecord.getInstitutionalImg());member.setTaxImg(memberRecord.getTaxImg());
        member.setBrandRegisterImg(memberRecord.getBrandRegisterImg());member.setBusinessBackImg(memberRecord.getBusinessBackImg());member.setPatentImg(memberRecord.getPatentImg());
        member.setIsLicense(memberRecord.getIsLicense());member.setIsInstitution(memberRecord.getIsInstitution());member.setIsTax(memberRecord.getIsTax());
        member.setIsBrandRegister(memberRecord.getIsBrandRegister());member.setIsBusinessBack(memberRecord.getIsBusinessBack());member.setIsPatent(memberRecord.getIsPatent());
        //项目部分
        product.setName(productRecord.getName());product.setViceName(productRecord.getViceName());
        product.setKeyWord(productRecord.getKeyWord());product.setNumber(productRecord.getNumber());product.setCategoryId(productRecord.getCategoryId());
        product.setCategoryName(productRecord.getCategoryName());product.setParentId(productRecord.getParentId());product.setParentName(productRecord.getParentName());
        product.setStandard(productRecord.getStandard());product.setType(productRecord.getType());product.setImage(productRecord.getImage());
        product.setStatus(2);product.setJoinArea(productRecord.getJoinArea());product.setIsAreaAuthorization(productRecord.getIsAreaAuthorization());
        product.setDirectShopNum(productRecord.getDirectShopNum());product.setJoinShopNum(productRecord.getJoinShopNum());product.setIsBusinessBack(productRecord.getIsBusinessBack());
        product.setBrandSource(productRecord.getBrandSource());product.setInvestAmount(productRecord.getInvestAmount());product.setLimitTime(productRecord.getLimitTime());
        product.setProducts(productRecord.getProducts());product.setJoinModel(productRecord.getJoinModel());product.setManagerModel(productRecord.getManagerModel());
        product.setFitPerson(productRecord.getFitPerson());product.setConsultLine(productRecord.getConsultLine());product.setJoinUrl(productRecord.getJoinUrl());
        product.setDesc(productRecord.getDesc());product.setJoinFee(productRecord.getJoinFee());product.setJoinAdvantage(productRecord.getJoinAdvantage());product.setJoinProcess(productRecord.getJoinProcess());product.setDetail(productRecord.getDetail());
        memberService.updateExceptEmpty(member.getId(),member);
        productService.update(product);


    }

    /**
     * 企业资质审核
     * @param memberRecordId
     * @param type
     */
    public void handleAptitude(Long memberRecordId,Integer type){
        SundayMemberRecord memberRecord=memberRecordService.getById(memberRecordId);
        if(type==1){
            memberRecord.setIsLicense(1);
        }else if(type==2){
            memberRecord.setIsInstitution(1);
        }else if(type==3){
            memberRecord.setIsTax(1);
        }else if(type==4){
            memberRecord.setIsBrandRegister(1);
        }else if(type==5){
            memberRecord.setIsBusinessBack(1);
        }else if(type==6){
            memberRecord.setIsPatent(1);
        }
        memberRecordService.updateExceptEmpty(memberRecordId,memberRecord);
    }
}
